package com.nd.android.u.ui.activity.recent_contact;

import java.util.Date;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.StarAppConfig;
import com.common.utils.StarAppUtils;
import com.nd.android.u.business.db.dao.RecentContactRecords;
import com.nd.android.u.controller.MessageConst.MessageState;
import com.nd.android.u.controller.bean.contact.RecentContactItem;
import com.nd.android.u.controller.bean.event.UnreadCountChangedEvent;
import com.nd.android.u.ui.R;
import com.nd.android.u.ui.chatUiUtils.ChatTimeUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import de.greenrobot.event.EventBus;

/**
 * 录音消息类型的Item
 * 
 * <br>Created 2014-10-17 下午2:33:22
 * @version  
 * @author   HuangYK，CB
 *
 * @see
 */
public class RecentContactAudioItemView extends RelativeLayout {

	private View mRoot;
	private ImageView mIvPic;
	private TextView mTvUnreadMsgCount;
	private TextView mTvLatestMsgTime;
	private TextView mTvMsgTitle;
	private ImageView mIvMsgFlag;
	/** 录音的长度*/
	private TextView mTvAudiolen;
	private Button mBtnDel;
	public RecentContactItem mitem;

	public RecentContactAudioItemView(Context context) {
		super(context);
		init();
	}

	private void init() {
		mRoot = LayoutInflater.from(getContext()).inflate(
				R.layout.recent_contact_audio_item, this);
		mIvPic = (ImageView) mRoot.findViewById(R.id.pic);
		mTvUnreadMsgCount = (TextView) mRoot
				.findViewById(R.id.msg_unread_count);
		mTvLatestMsgTime = (TextView) mRoot.findViewById(R.id.latest_msg_time);
		mTvMsgTitle = (TextView) mRoot.findViewById(R.id.msg_title);
		mIvMsgFlag = (ImageView) mRoot.findViewById(R.id.send_msg_failed);
		mBtnDel = (Button) mRoot.findViewById(R.id.del);
		mTvAudiolen =(TextView) mRoot.findViewById(R.id.audio_len_tv);
		initTypeface();
	}
	
	/**
	 * 初始化字体
	 * 
	 * <br>Created 2014-8-13 下午10:31:10
	 * @author        :  HuangYK
	 */
	private void initTypeface(){
		StarAppUtils.setViewTypeFace(mTvUnreadMsgCount, false);
        StarAppUtils.setViewTypeFace(mTvLatestMsgTime, false);
        StarAppUtils.setViewTypeFace(mTvMsgTitle, false);
        StarAppUtils.setViewTypeFace(mTvAudiolen, false);
        StarAppUtils.setViewTypeFace(mBtnDel, false);
	}

	public void updateContent(final Context ctx,  RecentContactItem item,
			boolean isNormalUse, boolean showDel) {
		this.mitem=item;
		ImageLoader.getInstance().cancelDisplayTask(mIvPic);
        mIvPic.setPadding(4, 4, 4, 4);

        if(StarAppConfig.getConfig(StarAppConfig.JAY_UID).equals(item.getId())){
            mIvPic.setBackgroundResource(R.drawable.bg_avator_of_star_frame);
            mTvMsgTitle.setTextColor(getResources().getColor(R.color.common_name_of_star_color));
        }else{
            mIvPic.setBackgroundResource(R.drawable.common_avator_translucent_frame);
            mTvMsgTitle.setTextColor(getResources().getColor(R.color.white));
        }

		mitem.displayHeadPortrait(mIvPic);

		mTvMsgTitle.setText(mitem.getMsgTitle(ctx));

		if (isNormalUse) {
			mTvUnreadMsgCount.setVisibility(View.VISIBLE);
			setUnreadMsgCount(mitem.getUnreadCount());
		} else {
			mTvUnreadMsgCount.setVisibility(View.GONE);
		}
		mTvLatestMsgTime.setText(ChatTimeUtils.parseDate(new Date(mitem.getLastContactTime()), 1));
		
		int state = mitem.getLastMsgState();
		int visibility;
		if (MessageState.isFailed(state)) {
			mIvMsgFlag.setImageResource(R.drawable.send_msg_failed);
			visibility = View.VISIBLE;
		} else if (MessageState.isSuccess(state)) {
			visibility = View.GONE;
		} else {
			mIvMsgFlag.setImageResource(R.drawable.sending_msg);
			visibility = View.VISIBLE;
		}
		mIvMsgFlag.setVisibility(visibility);
		
		
		
		
		
		
		
		int voicelen=mitem.getduration();
		Log.e("HYK", "voicelen = " + voicelen);
		if(voicelen>0){			
			mTvAudiolen.setText(mitem.getduration()+"\"");
		}
        /**  暂时屏蔽点击事件 */
		mIvPic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mitem.onClickHeadPortrait(ctx);
			}
		});

		showDelBtn(showDel);
		mBtnDel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDelBtn(false);
				RecentContactRecords.INSTANCE.deleteItem(mitem);
				EventBus eb = EventBus.getDefault();
				eb.post(new UnreadCountChangedEvent());
				if (RecentContactRecords.INSTANCE.isEmpty()) {
					eb.post(RecentContactRecordActivity.RECENT_CONTACT_UPDATE_UI);
				}
			}
		});
	}


	private void setUnreadMsgCount(int count) {
		if (count > 0) {
			mTvUnreadMsgCount.setVisibility(View.VISIBLE);

			final int max = 99;
			String text;
			if (count > max) {
//				text = "N";
				text = "…";
			} else {
				text = String.valueOf(count);
			}
			mTvUnreadMsgCount.setText(text);
		} else {
			mTvUnreadMsgCount.setVisibility(View.GONE);
		}
	}

	private void showDelBtn(boolean show) {
		int currentVisibility = mBtnDel.getVisibility();
		if (show) {
			if (currentVisibility == View.VISIBLE) {
				return;
			}
			mBtnDel.setVisibility(View.VISIBLE);
			mTvLatestMsgTime.setVisibility(View.INVISIBLE);
		} else {
			if (currentVisibility != View.VISIBLE) {
				return;
			}
			RecentContactListFragment.HAS_DEL_BTN_ITEM_ID = "";
			mBtnDel.setVisibility(View.GONE);
			mTvLatestMsgTime.setVisibility(View.VISIBLE);
		}
	}
	
	public void initTitleByUid(String name){
		mTvMsgTitle.setText(name);
	}


	/**
	 * 设置位置信息
	 * 
	 * <br>
	 * Created 2014-10-17 上午10:31:46
	 * 
	 * @author : HuangYK
	 */
	public void setPosition(int position) {
		if (position % 2 == 0) {
			setBackgroundResource(R.drawable.common_item_transparent_2_translucent_white);
		} else {
			setBackgroundResource(R.drawable.common_item_translucent_white_2_transparent);
		}
	}
}
